/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FileRecvFrame.java
 *
 * Created on 2011-11-6, 10:44:51
 */

package com.lanchat.view;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author GM
 */
public class FileRecvFrame extends javax.swing.JFrame implements Runnable{
    private int port;
    private String filename;
    private int length;
    /** Creates new form FileRecvFrame */
    public FileRecvFrame() {
        initComponents();
        this.setVisible(true);
    }

    public FileRecvFrame(String info,int port){
        this.port=port;
        int split=info.indexOf('#');
        filename=info.substring(0, split);
        length=Integer.parseInt(info.substring(split+1,info.length()));

        initComponents();
        
        FName.setText("文件名："+filename);
        Progress.setMaximum(length);
        Thread t=new Thread(this);
        t.start();
        MainListFrame.fileRecv.put(filename, this);
        this.setVisible(true);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        FName = new javax.swing.JLabel();
        Progress = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        FName.setText("文件名:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(FName)
                    .addComponent(Progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(FName)
                .addGap(18, 18, 18)
                .addComponent(Progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        MainListFrame.fileRecv.remove(filename);
    }//GEN-LAST:event_formWindowClosing

    /**
    * @param args the command line arguments
    */
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel FName;
    private javax.swing.JProgressBar Progress;
    // End of variables declaration//GEN-END:variables

    @Override
    public void run() {
        try {
            ServerSocket S = new ServerSocket(port);
            Socket socket=S.accept();
            BufferedInputStream bin=new BufferedInputStream(socket.getInputStream());
            File f=new File(".\\filerecv\\"+filename);
            f.createNewFile();
            FileOutputStream fout=new FileOutputStream(f);
            int ch;
            while((ch=bin.read())!=-1){
                fout.write(ch);
                Progress.setValue(Progress.getValue()+1);
            }
            fout.close();
            bin.close();
            socket.close();
            JOptionPane.showMessageDialog(null, "接收成功！");
            this.dispose();
        } catch (IOException ex) {
            Logger.getLogger(FileRecvFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}
